package com.example.user.myhjjf_app.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.example.user.myhjjf_app.HJJF_Url;
import com.example.user.myhjjf_app.R;
import com.example.user.myhjjf_app.model.Model_Regional;
import com.example.user.myhjjf_app.model.Model_zhcsg;
import com.example.user.myhjjf_app.presenter.Presenter_Csg_adaoter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.zhy.adapter.abslistview.MultiItemTypeAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Activity_zhcsgAdd extends AppCompatActivity {

    MultiItemTypeAdapter adapter;
    MultiItemTypeAdapter adapter2;
    Model_zhcsg zhcsg;
    List<String> nums = new ArrayList<>();
    int type=1;
    int type2=0;
    Map<String,Model_zhcsg.ValueBean> map = new HashMap<>();
    Map<String,Model_zhcsg.ValueBean> map2 = new HashMap<>();
    @BindView(R.id.btn_back)
    LinearLayout btnBack;
    @BindView(R.id.tx_title)
    TextView txTitle;
    @BindView(R.id.btn_menu)
    LinearLayout btnMenu;
    @BindView(R.id.gridView1)
    GridView gridView1;
    @BindView(R.id.gridView2)
    GridView gridView2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhcsg_add);
        ButterKnife.bind(this);
        txTitle.setText("综合传输柜");
        for (int i = 0; i < 6; i++) {
            nums.add(String.valueOf(i));
        }
        adapter = new MultiItemTypeAdapter(this,nums);
        adapter.addItemViewDelegate(new Presenter_Csg_adaoter(type, map));
        gridView1.setAdapter(adapter);
        adapter2 = new MultiItemTypeAdapter(this,nums);
        adapter2.addItemViewDelegate(new Presenter_Csg_adaoter(type2, map2));
        gridView2.setAdapter(adapter2);
        init();

        gridView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(Activity_zhcsgAdd.this,Activity_zhDkAdd.class);
                Bundle bundle = new Bundle();
                if(map.containsKey(String.valueOf(i))){
                    bundle.putString("type","select");
                    bundle.putSerializable("va",map.get(String.valueOf(i)));
                    bundle.putString("tj","gw1");
                }else {
                    bundle.putString("type","add");
                    bundle.putString("tj","gw1");
                }
                bundle.putInt("position",i+1);
                intent.putExtras(bundle);
                startActivityForResult(intent,0);
            }
        });
        gridView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(Activity_zhcsgAdd.this,Activity_zhDkAdd.class);
                Bundle bundle = new Bundle();
                if(map2.containsKey(String.valueOf(i))){
                    bundle.putString("type","select");
                    bundle.putSerializable("va",map2.get(String.valueOf(i)));
                    bundle.putString("tj","gw2");
                }else {
                    bundle.putString("type","add");
                    bundle.putString("tj","gw2");
                }
                bundle.putInt("position",i);
                intent.putExtras(bundle);
                startActivityForResult(intent,0);
            }
        });

    }

    private void init(){
        OkGo.<String>post(HJJF_Url.getHJJF_Url().login("api/site/odp_list.do"))
                .tag(this)
                .params("id", Model_Regional.getRegional().getSiteId())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        map.clear();
                        zhcsg = JSON.parseObject(response.body(), Model_zhcsg.class);
                        for (int i = 0; i < zhcsg.getValue().size(); i++) {
                            map.put(String.valueOf(zhcsg.getValue().get(i).getPosition()-1),zhcsg.getValue().get(i));
                        }
                        adapter.notifyDataSetChanged();
                    }
                });
        OkGo.<String>post(HJJF_Url.getHJJF_Url().login("api/site/ddf_list.do"))
                .tag(this)
                .params("id", Model_Regional.getRegional().getSiteId())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        map2.clear();
                        zhcsg = JSON.parseObject(response.body(), Model_zhcsg.class);
                        for (int i = 0; i < zhcsg.getValue().size(); i++) {
                            map2.put(String.valueOf(zhcsg.getValue().get(i).getPosition()),zhcsg.getValue().get(i));
                        }
                        adapter2.notifyDataSetChanged();
                    }
                });
    }


    @OnClick(R.id.btn_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==1){
            init();
        }
    }
}
